﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using StoneFish;

namespace StoneFishDemo
{
    public partial class FrmWizard : Form
    {


        OpenFileDialog ofd = new OpenFileDialog();
        enum operatin { Hide, Read }
        operatin Op;

        Stack<int> StackPrev = new Stack<int>();
        Stack<int> StackNext = new Stack<int>();
        Stack<int> Stack1 = new Stack<int>();
        Stack<int> Stack2 = new Stack<int>();


        private void AdjustButtons()
        {
            if (StackNext.Count == 0) btnNext.Enabled = false;
            else btnNext.Enabled = true;

            if (StackPrev.Count == 0) btnPrev.Enabled = false;
            else btnPrev.Enabled = true;
        }

        private void InitParams()
        {
            Op = operatin.Hide;
            InitNavQs();
            SetNavQ(Op);
            AdjustButtons();
        }

        private void InitNavQs()
        {
            Stack1.Clear();
            Stack2.Clear();
            //Initialise UI hiding  navigation stack
            Stack1.Push(4);
            Stack1.Push(3);
            Stack1.Push(2);
            Stack1.Push(1);
            //Initialise UI reading navigairion Queue
            Stack2.Push(5);
            Stack2.Push(6);
            Stack2.Push(1);


        }
        private void SetNavQ(operatin opType)
        {
            switch (opType)
            {
                case operatin.Hide: StackNext = Stack1; break;
                case operatin.Read: StackNext = Stack2; break;
                default: break;
            }
        }


        public FrmWizard()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            ofd.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            pb1.Image = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StackNext.Push(0);
            wizard1.SelectTab(1);

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (rdBtnHide.Checked)
            {
                Op = operatin.Hide;
                wizard1.SelectTab(2);
            }
            else
            {
                Op = operatin.Read;
                wizard1.SelectTab(6);
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            wizard1.SelectedIndex = 3;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            wizard1.SelectedIndex = 4;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            wizard1.SelectedIndex = 5;
        }


        private void rdBtnHide_CheckedChanged(object sender, EventArgs e)
        {
            Op = operatin.Hide;
            InitNavQs();
            SetNavQ(Op);
        }

        private void rdBtnRead_CheckedChanged(object sender, EventArgs e)
        {
            Op = operatin.Read;
            InitNavQs();
            SetNavQ(Op);
        }

        private void FrmWizard_Load(object sender, EventArgs e)
        {
            ofd.FileOk += Ofd_FileOk;
            
            InitParams();
        }
      
        private void Ofd_FileOk(object sender, CancelEventArgs e)
        {
            pb1.ImageLocation = ofd.FileName;
            txtPath.Text = ofd.FileName;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.Validate())
                {
                    StackPrev.Push(wizard1.SelectedIndex);
                    int num = 0;
                    do
                    {
                        num = StackNext.Pop();

                    } while (StackPrev.Contains(num));
                    wizard1.SelectTab(num);
                    AdjustButtons();
                    progressBar1.Value = (int)(((double)StackPrev.Count / (StackPrev.Count + StackNext.Count)) * 100);

                }
                else return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

      
    
        private void btnPrev_Click(object sender, EventArgs e)
        {
            try
            {
                this.AutoValidate = AutoValidate.EnableAllowFocusChange;
                StackNext.Push(wizard1.SelectedIndex);
                wizard1.SelectTab(StackPrev.Pop());
                AdjustButtons();
                progressBar1.Value = (int)(((double)StackPrev.Count / (StackPrev.Count + StackNext.Count)) * 100);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void textBox3_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = pb1.Image == null;
            if (e.Cancel) displayError(sender, "Select a valid picture");
            else ClearError();
        }

        private void displayError(object sender, string msg)
        {
            errorProvider1.SetError((Control) sender, msg);
          
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            e.Cancel = false;
            base.OnFormClosing(e);
        }
        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
        }


        private void tabPage3_Enter(object sender, EventArgs e)
        {
            txtPath.Focus();
        }

        private void txtMark_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = string.IsNullOrEmpty(txtText.Text);
            if (e.Cancel) displayError(sender, "Enter a valid text to hide");
            else ClearError();
        }

        private void txtKey_Validating(object sender, CancelEventArgs e)
        {
            int rslt;
            string text = ((TextBox)sender).Text;
            e.Cancel =  (!int.TryParse(text, out rslt) || text.Length < 6);
            if (e.Cancel) displayError(sender, "Enter a 6 digit PIN");
            else ClearError();


        }
        private void ClearError()
        {
            errorProvider1.Clear();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            svd.DefaultExt = ".png";
            svd.AddExtension = true;
            svd.FileName = "ResultImage";
            svd.ShowDialog();
        }

        private void svd_FileOk(object sender, CancelEventArgs e)
        {
           
            using (FileStream fs = new FileStream(svd.FileName, FileMode.Create))
            {
                pb1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp);
            }
           
        }

        private void txtPath_Click(object sender, EventArgs e)
        {
            ofd.ShowDialog();
        }
        private void button1_Click_2(object sender, EventArgs e)
        {
            RenederResultImage();
        }
        private void RenederResultImage()
        {
            Bitmap orgImage = (Bitmap)pb1.Image;
            string hideText = txtText.Text;
            string PIN = txtPinHide.Text;
            TextHider ImgHider = new TextHider(orgImage, PIN);
            Bitmap rsltImage = ImgHider.HideText(hideText);
            pbRslt.Image = rsltImage;
        }

        private void tabPage3_Enter_1(object sender, EventArgs e)
        {
            txtPath.Focus();
        }

        private void tabPage8_Enter(object sender, EventArgs e)
        {
            ExtractHiddenText();
        }

        private void ExtractHiddenText()
        {
            Bitmap orgImage = (Bitmap)pb1.Image;
            string PIN = txtPINRead.Text;
            TextHider ImgHdr = new TextHider(orgImage, PIN);
            string hiddenText = ImgHdr.ExtractText();
            txtRsltText.Text = hiddenText;
        }

        private void button1_Click_3(object sender, EventArgs e)
        {
            ofd.ShowDialog();
        }

        private void tabPage3_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = pb1.Image == null;
            if (e.Cancel) displayError(pb1, "Select a valid picture");
            else ClearError();
        }
    }
}
